<?php
//session_start();
function checkUser($username,$password){
		  
	$query = "SELECT * FROM `client_logininfo` l JOIN `crm_contact` c ON c.ContactID = l.OriginalContact_id  WHERE l.email_address = '".$username."' and l.password = '".md5($password)."' AND c.Sacc_accountlock <> '1';";
	//echo $query; //exit;
	$res = mysql_query($query);
	$num_rows = mysql_num_rows($res);
       
      
	if($num_rows > 0)
	{
		while($row = mysql_fetch_array($res))
		{
                    
            //echo '<pre>'; print_r($row); exit;
			$sqlRes = mysql_query("SELECT app_id FROM crm_applicationcontact WHERE ContactID = ".$row['OriginalContact_id']." ORDER BY ApplicationcontactID DESC LIMIT 1");
			$resData = mysql_fetch_assoc($sqlRes);
                       
			$_SESSION['display_name'] = $row['display_name'];
			$_SESSION['app_email_addr'] = $row['email_address'];
			$_SESSION['applicant_id'] = $row['OriginalContact_id'];
			$_SESSION['CurrentContact_id'] = $row['CurrentContact_id'];
			$_SESSION['app_id'] = $resData['app_id'];

		}    
		saveLoginHistory();
		return true;
	}
	else
	{
		return false;
	}
}

function saveLoginHistory()
{
	 $login_history_query = "INSERT INTO `client_loginhistory` (`CurrentContact_id`,`OriginalContact_id`,`display_name`,`email_address`,`LoginDate`, `LoginTime`, `LoginIP`, `LoginBrowser`, `Comesfrom`) VALUES ('".$_SESSION['CurrentContact_id']."', '".$_SESSION['applicant_id']."', '".$_SESSION['display_name']."', '".$_SESSION['app_email_addr']."', DATE(NOW()), CURTIME(), '".$_SERVER['REMOTE_ADDR']."', '".$_SERVER['HTTP_USER_AGENT']."', 'MaxCash');";
	 	  
	if(!mysql_query($login_history_query));
		echo mysql_error();
}
function getApplication($app_id)
{
	$loan_app = "SELECT * FROM crm_application WHERE app_id = ".$app_id;
	$res = mysql_query($loan_app);
	$num = mysql_num_rows($res);
	$applicarion = mysql_fetch_assoc($res);
	if(isset($num) && $num > 0)	{
		return $applicarion;
	}else{
		return false;  
	}
}
?>